package com.zhuangfei.hpuhelper.view;

import java.util.ArrayList;
import java.util.List;

import com.hpu.bean.Course;
import com.hpu.bean.CourseListEntity;
import com.hpu.constants.ShareConstants;
import com.hpu.tools.CourseTools;
import com.hpu.tools.ShareTools;
import com.hpu.tools.TimeTools;
import com.zhuangfei.hpuhelper.tools.ActivityTools;
import com.zhuangfei.hpuhelper.tools.ReflectTools;
import com.zhuangfei.timetable.core.SubjectUIModel;
import com.zhuangfei.timetable.core.SubjectUtils;

import cn.lzf.serviceplatform.R;
import android.R.integer;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class CourseDetailActivity extends Activity {

	private CourseListEntity lists;
	
	private TextView nameTextView;
	private TextView roomTextView;
	private TextView weeksTextView;
	private TextView dayTextView;
	private TextView teacherTextView;
	private LinearLayout backLayout;
	
	private LinearLayout container;
	
	private LayoutInflater inflater;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_course_detail);
		ActivityTools.setTransparent(this);
		checkData();
		initView();
		initEvent();
		loadData();
	}

	private void checkData() {
		Intent intent=getIntent();
		lists=(CourseListEntity) intent.getSerializableExtra(ReflectTools.EXTRAS_COURSE);
		if (lists==null||lists.getResult()==null) {
			Toast.makeText(this,"课程页传值异常!", Toast.LENGTH_SHORT).show();
			goBack();
		}
	}

	private void loadData() {
		String start = ShareTools.get(this, ShareConstants.STORE_WEEK_STARTTIME, "").toString();
		int cur_week=-1;
		if(start!=null&&start.length()!=0){
			cur_week = TimeTools.timeTransfrom(start);
		}
		
		List<Course> list=new ArrayList<>();
		for(int i=0;i<lists.getResult().size();i++){
			Course course=lists.getResult().get(i);
			if(course.getWeekList().indexOf(cur_week)==-1){
				list.add(course);
				continue;
			}
			View view=createView(cur_week,course);
			container.addView(view);
		}
		
		for(int i=0;i<list.size();i++){
			Course course=list.get(i);
			View view=createView(cur_week,course);
			
			container.addView(view);
		}
		
	}

	private View createView(int cur_week,Course course) {
		View view=inflater.inflate(R.layout.item_course_detail_layout,null, false);
		nameTextView=(TextView) view.findViewById(R.id.id_coursedetail_name);
		roomTextView=(TextView) view.findViewById(R.id.id_coursedetail_room);
		weeksTextView=(TextView) view.findViewById(R.id.id_coursedetail_weeks);
		dayTextView=(TextView) view.findViewById(R.id.id_coursedetail_day);
		teacherTextView=(TextView) view.findViewById(R.id.id_coursedetail_teacher);
		
//		System.out.println(course.getColorRandom());
//		container.setBackgroundResource(SubjectUIModel.getBackgroundByRandom(course.getColorRandom()));
//		container.setBackgroundColor(SubjectUIModel.getBackgroundByRandom(course.getColorRandom()));

		if(course==null){
			nameTextView.setText("未知异常");
			roomTextView.setText("未知异常");
			weeksTextView.setText("未知异常");
			dayTextView.setText("未知异常");
			teacherTextView.setText("未知异常");
			return  view;
		}

		if(TextUtils.isEmpty(course.getName())){
			nameTextView.setText("--");
		}else{
			nameTextView.setText(course.getName());
			if(course.getWeekList().indexOf(cur_week)!=-1){
				nameTextView.setText(course.getName()+"(本周)");
			}
		}

		if(TextUtils.isEmpty(course.getRoom())){
			roomTextView.setText("教室\t--");
		}else{
			roomTextView.setText("教室\t"+course.getRoom());
		}
		

		String s="";
		List<Integer> weekList=course.getWeekList();

		if(weekList==null){
			weeksTextView.setText("周次\t--");
		}else{
			for(int j=0;j<weekList.size();j++){
				s+=weekList.get(j)+",";
			}
			weeksTextView.setText("周次\t"+s);
		}

		if(course.getDay()<=0||course.getStart()<=0||course.getStep()<=0){
			dayTextView.setText("节次\t--");
		}else{
			dayTextView.setText("节次\t周"+getDay(course.getDay())+",\t"+course.getStart()+"-"+(course.getStart()+course.getStep()-1)+"节");
		}

		if (TextUtils.isEmpty(course.getTeacher())) {
			teacherTextView.setText("教师\t--");
		}else{
			teacherTextView.setText("教师\t"+course.getTeacher());
		}
		return view;
	}

	public String getDay(int day){
		String str="一二三四五六七";
		return str.charAt(day-1)+"";
	}
	
	private void initView() {
		container=(LinearLayout) findViewById(R.id.id_container);
		inflater=LayoutInflater.from(this);
		backLayout=(LinearLayout) findViewById(R.id.id_back);
	}

	private void initEvent() {
		backLayout.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				goBack();
			}
		});
	}
	
	public void goBack(){
		ActivityTools.toBackActivityAnim(this,MainActivity.class);
	}
	
	@Override
	public void onBackPressed() {
		goBack();
	}
}
